const Router = require('koa-router')
const comments = new Router()
const db = require('../mysql/index');
const bodyparser = require("koa-body");
comments.use(bodyparser())


comments.post('/send', async (ctx)=>{
    let username = ctx.request.body.username
    let comments_word = ctx.request.body.commentsword
    let game_id = ctx.request.body.gameid
    let game_name = ctx.request.body.gamename
    let user_id = ctx.request.body.userid
    let comment = ctx.request.body.comment
    let now = (new Date()).Format("yyyyMMdd")
    const sql =`INSERT INTO comments (user_name,comments_word,comments_time,game_id,game_name,user_id,comments)
                VALUES ('${username}','${comments_word}',${now},${game_id},'${game_name}','${user_id}','${comment}');`
    console.log(sql)
    db.query(sql,[],(res,err)=>{
        console.log(res,err)
    })
    ctx.body = await new Promise((resolve, reject) => {
        return db.query(`SELECT * FROM comments`,[], (data) => {
            resolve(data)
        })
    })
})


module.exports = comments
